﻿using System;
using _5E.Utils.CollectionHelper;

namespace WholesaleOnline.Controller
{
    public class MySession : IGenericCache
    {
        private IGenericCache m_cache = new DictionaryCache();
        private DateTime m_lastModify = DateTime.Now;

        public bool IsTimeout
        {
            get
            {
                Int32 interval = Convert.ToInt32((DateTime.Now - m_lastModify).TotalMinutes);
                return s_Timeout <= interval;
            }
        }

        private static Int32 s_Timeout = 5;
        public static Int32 Timeout { set { s_Timeout = value; } }

        public void Add<T>(string key, T value)
        {
            m_cache.Add(key, value);
        }

        public bool ContainsKey<T>(string key)
        {
            return m_cache.ContainsKey<T>(key);
        }

        public bool ContainsValue<T>(T value)
        {
            return m_cache.ContainsValue(value);
        }

        public T GetValue<T>(string key)
        {
            return m_cache.GetValue<T>(key);
        }

        public void Remove<T>(string key)
        {
            m_cache.Remove<T>(key);
        }

        public bool TryGetValue<T>(string key, out T value)
        {
            return m_cache.TryGetValue(key, out value);
        }

        public void Reset()
        {
            m_lastModify = DateTime.Now;
        }

        public void Clear()
        {
            m_cache.Clear();
            Reset();
        }

        public void Merge<T>(string key, T value)
        {
            m_cache.Merge(key, value);
        }
    }
}
